package com.me.db;

import java.util.ArrayList;
import java.util.TimeZone;

import com.me.data.MeEvent;
import com.me.util.AppUtil;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Instances;

public class CalendarManager {

	private ContentResolver contentResolver;

	public CalendarManager(ContentResolver contentResolver) {
		this.contentResolver = contentResolver;
	}

	public Cursor fetchCalendar() {

		Cursor cursor = null;
		Uri uri = Calendars.CONTENT_URI;

		cursor = contentResolver.query(uri, CalendarQuery.Calendar.PROJECTION,
				CalendarQuery.Calendar.SELECTION,
				CalendarQuery.Calendar.ARGUMENT,
				CalendarQuery.Calendar.COLUMN_ID
						+ CalendarQuery.ORDER_ASCENDING);

		return cursor;
	}

	public Cursor fetchInstance(long start, long end) {
		Cursor cursor = null;

		Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
		ContentUris.appendId(builder, start);
		ContentUris.appendId(builder, end);

		 cursor = contentResolver.query(builder.build(),
		 CalendarQuery.Instance.PROJECTION,
		 CalendarQuery.Instance.SELECTION,
		 new String[] { AppUtil.App.currCalendarId },
		 CalendarQuery.Instance.COLUMN_ID
		 + CalendarQuery.ORDER_ASCENDING);

		// for testing
//		cursor = contentResolver.query(builder.build(),
//				CalendarQuery.Instance.PROJECTION, null, null,
//				CalendarQuery.Instance.COLUMN_ID
//						+ CalendarQuery.ORDER_ASCENDING);

		return cursor;
	}

	public void insertOneEvent(String inputEventTitle, String inputEventVenue,
			int allDay, long inputEventStart, long inputEventEnd) {

		ContentValues values = new ContentValues();

		values.put(Events.TITLE, inputEventTitle);
		values.put(Events.EVENT_LOCATION, inputEventVenue);
		values.put(Events.ALL_DAY, allDay);
		values.put(Events.DTSTART, inputEventStart);
		values.put(Events.DTEND, inputEventEnd);
		values.put(Events.CALENDAR_ID, AppUtil.App.currCalendarId);
		values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

		contentResolver.insert(Events.CONTENT_URI, values);

	}

	public int deleteEvents(String inClause, String[] eventIDs) {
		return contentResolver.delete(Events.CONTENT_URI, Events._ID + " in "+ inClause, eventIDs);
	}

	public int insertEvents(ArrayList<MeEvent> eventList) {
		ContentValues[] values = new ContentValues[eventList.size()];
		for (int i=0; i<eventList.size(); i++) {
			MeEvent event = eventList.get(i);
			ContentValues temp = values[i] = new ContentValues();
			temp.put(Events.TITLE, event.getTitle());
			temp.put(Events.EVENT_LOCATION, event.getVenue());
			if (event.isAllDay())
				temp.put(Events.ALL_DAY, 1);
			else
				temp.put(Events.ALL_DAY, 0);
			temp.put(Events.DTSTART, event.getStart());
			temp.put(Events.DTEND, event.getEnd());
			temp.put(Events.CALENDAR_ID, AppUtil.App.currCalendarId);
			temp.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
		}
		return contentResolver.bulkInsert(Events.CONTENT_URI, values);
	}
	
	
	public Cursor searchCalendarEvents(long start, long end, String field, String keyword) {
		Cursor cursor = null;
		
		Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
		ContentUris.appendId(builder, start);
		ContentUris.appendId(builder, end);		

		cursor = contentResolver.query(
				 builder.build(),
				 CalendarQuery.Instance.PROJECTION,
				 CalendarQuery.Instance.SELECTION + " AND " + field + " LIKE ?",
				 new String[] { AppUtil.App.currCalendarId, "%"+keyword+"%" },
				 CalendarQuery.Instance.COLUMN_ID + CalendarQuery.ORDER_ASCENDING);
		 
		 return cursor;
	}
	
}
